package cn.wangdian.erp.demo;

import cn.wangdian.erp.sdk.Pager;
import cn.wangdian.erp.sdk.api.goods.GoodsAPI;
import cn.wangdian.erp.sdk.impl.ApiFactory;
import cn.wangdian.erp.sdk.impl.DefaultClient;
import cn.wangdian.erp.sdk.Client;
import cn.wangdian.erp.sdk.WdtErpException;
import cn.wangdian.erp.sdk.api.goods.dto.ApiGoodsSearchRequest;
import cn.wangdian.erp.sdk.api.goods.dto.ApiGoodsSearchResponse;
import cn.wangdian.erp.sdk.api.goods.dto.ApiGoodsUploadRequest;
import cn.wangdian.erp.sdk.api.goods.dto.ApiGoodsUploadResponse;
import cn.wangdian.erp.sdk.api.goods.dto.GoodsBatchPushRequest;
import cn.wangdian.erp.sdk.api.goods.dto.GoodsBatchPushResponse;
import cn.wangdian.erp.sdk.api.goods.dto.GoodsPushRequest;
import cn.wangdian.erp.sdk.api.goods.dto.GoodsSearchRequest;
import cn.wangdian.erp.sdk.api.goods.dto.GoodsSearchResponse;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class Goods
{
	public static void main(String[] args) throws WdtErpException
	{
		// Client client = DefaultClient.get("wdtapi3", "test", "test");
		Client client = DefaultClient.get("wdterp30", "http://172.172.1.62:30000/", "xyx_api",
				"e8866c1681dacc9488a19c89991791b8:8f5800dab84b7a8d614e92cba739032c");
		GoodsAPI goodsApi = ApiFactory.get(client, GoodsAPI.class);

//		goodsPushTest(goodsApi);

//		GoodsSearchTest(goodsApi);

//		ApiGoodsSearchTest(goodsApi);
//		ApiGoodsUploadTest(goodsApi);
		batchPushTest(goodsApi);
	}

	private static void goodsPushTest(GoodsAPI goodsApi)
	{
		GoodsPushRequest.GoodsDto goodsDto = new GoodsPushRequest.GoodsDto();
		goodsDto.setGoodsNo("test_lz123");
		goodsDto.setGoodsName("test_lz123");

		GoodsPushRequest.GoodsSpecDto goodsSpecDto = new GoodsPushRequest.GoodsSpecDto();
		goodsSpecDto.setSpecNo("test_lz123");
		goodsSpecDto.setBarcode("test_lz123");
		goodsSpecDto.setSpecName("test_lz123");
		goodsSpecDto.setMarketPrice(new BigDecimal("12"));
		goodsSpecDto.setRetailPrice(new BigDecimal("12"));
		List<GoodsPushRequest.GoodsSpecDto> goodsSpecDtos = new ArrayList<>();
		goodsSpecDtos.add(goodsSpecDto);

		int goodsId = goodsApi.push(goodsDto, goodsSpecDtos);
		System.out.println(goodsId);
	}

	private static void GoodsSearchTest(GoodsAPI goodsApi)
	{
		GoodsSearchRequest request = new GoodsSearchRequest();
		request.setSpecNo("daba3");
		GoodsSearchResponse response = goodsApi.search(request, new Pager(10, 0, true));

		if (response.getTotal() == null || response.getTotal() == 0)
		{
			System.out.println("No eligible results!");
			return;
		}

		System.out.println("total: " + response.getTotal());
		for (GoodsSearchResponse.GoodsSearchGoodsDto goodsDto : response.getGoodsInfos())
		{
			System.out.print("goods no:" + goodsDto.getGoodsNo() + " modified : " + goodsDto.getGoodsModified());
			for (GoodsSearchResponse.GoodsSearchSpecDto specDto : goodsDto.getSpecDtos())
			{
				System.out.println(" spec no:" + specDto.getSpecNo() + " modified : " + specDto.getSpecModified());
			}
		}
	}

	private static void ApiGoodsSearchTest(GoodsAPI goodsApi) throws WdtErpException
	{
		ApiGoodsSearchRequest request = new ApiGoodsSearchRequest();
		request.setStartTime("2022-11-01");
		request.setEndTime("2022-11-31");
		ApiGoodsSearchResponse response = goodsApi.searchApiGoods(request, new Pager(10, 0, true));

		if (response.getTotal() == null || response.getTotal() == 0)
		{
			System.out.println("No eligible results!");
			return;
		}

		System.out.println("total: " + response.getTotal());
		for (ApiGoodsSearchResponse.ApiGoods apiGoods : response.getGoodsList())
		{
			System.out.print("goods id:" + apiGoods.getGoodsId() + " modified : " + apiGoods.getModified());
		}
	}

	private static void ApiGoodsUploadTest(GoodsAPI goodsApi) throws WdtErpException
	{
		ApiGoodsUploadRequest request = new ApiGoodsUploadRequest();
		request.setShopNo("xyx_self");

		ApiGoodsUploadRequest.ApiGoods apiGoods = new ApiGoodsUploadRequest.ApiGoods();
		apiGoods.setSpecId("xyxtest_02");
		apiGoods.setGoodsNo("xyxtest_02");
		apiGoods.setGoodsId("xyxtest_02");
		apiGoods.setSpecNo("xyxtest_02");
		apiGoods.setStatus(0);
		apiGoods.setGoodsName("xyxtest_02");
		apiGoods.setSpecName("xyxtest_02");
		apiGoods.setPrice(new BigDecimal("2"));
		apiGoods.setStockNum(new BigDecimal("2"));

		request.setGoodsList(List.of(apiGoods));

		ApiGoodsUploadResponse response = goodsApi.uploadApiGoods(request);

		if (response == null || response.getStatus() > 0)
			System.out.println("error");
		else
			System.out.println("success");
	}

	private static void batchPushTest(GoodsAPI goodsApi) throws WdtErpException
	{

		List<GoodsBatchPushRequest.Goods.Spec> specList = new ArrayList<>();
		GoodsBatchPushRequest.Goods.Spec spec = new GoodsBatchPushRequest.Goods.Spec();
		specList.add(spec);

		spec.setSpecNo("apiBatchPushTest");

		List<GoodsBatchPushRequest.Goods> goodsList = new ArrayList<>();
		GoodsBatchPushRequest.Goods goods = new GoodsBatchPushRequest.Goods();
		goodsList.add(goods);
		goods.setSpecList(specList);

		goods.setGoodsNo("apiBatchPushGoods");
		goods.setGoodsName("apiBatchPushGoods");

		GoodsBatchPushResponse response = goodsApi.batchPush(goodsList);
		List<GoodsBatchPushResponse.Error> errorList = response.getErrorList();
		if (errorList.isEmpty())
		{
			System.out.println("success");
			return;
		}

		for (GoodsBatchPushResponse.Error error : errorList)
		{
			System.out.println("error goods_no: "+error.getNo()+", msg: "+error.getError());
		}
	}
}
